 package com.mbc.rmt.manager;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

import com.mbc.common.CommonConstants;
import com.mbc.common.CommonSingleton;
import com.mbc.common.dao.CommonDAOFactory;
import com.mbc.common.dao.CurrencyRateDAO;
import com.mbc.common.dao.LogDAO;
import com.mbc.common.entities.CurrencyRate;
import com.mbc.common.entities.Log;
import com.mbc.common.exception.DataNotFoundException;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.param.CriteriaParams;
import com.mbc.common.param.CriteriaRMParams;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.rmt.RemittanceConstants.RMTRecordStatus;
import com.mbc.rmt.dao.AddCodeDAO;
import com.mbc.rmt.dao.AddCodeDetailDAO;
import com.mbc.rmt.dao.BankDAO;
import com.mbc.rmt.dao.BranchCOADAO;
import com.mbc.rmt.dao.CashReceivedDownloadLogDAO;
import com.mbc.rmt.dao.General_COADAO;
import com.mbc.rmt.dao.RemittanceCashReceivedArchiveDAO;
import com.mbc.rmt.dao.RemittanceCashReceivedDAO;
import com.mbc.rmt.dao.RemittanceDAOFactory;
import com.mbc.rmt.data.AddCodeNDetail;
import com.mbc.rmt.data.CashReceivedDownloadLogDataList;
import com.mbc.rmt.data.RemittanceCashReceivedDataList;
import com.mbc.rmt.entities.AddCode;
import com.mbc.rmt.entities.AddCodeDetail;
import com.mbc.rmt.entities.Bank;
import com.mbc.rmt.entities.BranchCOA;
import com.mbc.rmt.entities.CashReceiptRpt;
import com.mbc.rmt.entities.CashReceivedDownloadLog;
import com.mbc.rmt.entities.GeneralLedger;
import com.mbc.rmt.entities.General_COA;
import com.mbc.rmt.entities.RemittanceCashReceived;

public class RemittanceCashReceivedMgr extends AbstractMgr {
	
	private RemittanceCashReceivedDAO rmtCashReceiveDAO;
	private General_COADAO generalCOADAO;
	private BranchCOADAO branchCOADAO;
	private CurrencyRateDAO currencyRateDAO;
	private LogDAO logDAO;
	
	public static enum GL_STATUS { CREATE, DELETE }
	
	public RemittanceCashReceivedMgr(){}
	
	public RemittanceCashReceivedMgr(String sesssionId){
		this.sessionId = sesssionId;
	}
	
	public String create(RemittanceCashReceived entity)throws Exception{
		openConnection();
		try {
			rmtCashReceiveDAO = RemittanceDAOFactory.getRemittanceCashReceivedDAO(getConnection());
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			
			if(!entity.getAddCode().equals("") && entity.getAddCodeId() == 0){
				AddCodeDAO l_AddCodeDAO = RemittanceDAOFactory.getAddCodeDAO(getConnection());
				AddCode l_AddCode = l_AddCodeDAO.findByCode(entity.getAddCode(), entity.getBranchId());
				if(l_AddCode == null)
					throw new DataNotFoundException("Add Code does not exist.", AddCode.class);
				entity.setAddCodeId(l_AddCode.getAddCodeId());
			} else if(entity.getAddCode().equals("")){
				AddCodeDAO l_AddCodeDAO = RemittanceDAOFactory.getAddCodeDAO(getConnection());
				AddCode l_AddCode = l_AddCodeDAO.findByCode("_", entity.getBranchId());
				if(l_AddCode == null)
					throw new DataNotFoundException("Add Code does not exist for common\"_\".", AddCode.class);
				entity.setAddCodeId(l_AddCode.getAddCodeId());
		
				l_AddCode.setCode("_");
				l_AddCode.setSender(entity.getSender());
				l_AddCode.setBranchId(entity.getBranchId());
				l_AddCodeDAO.update(l_AddCode);
				
				l_logentity.setTablename("AddCode");
				l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
				l_logentity.setKey(l_AddCode.getAddCodeId());
				l_logentity.setUserid(l_AddCode.getUserid());
				l_logentity.setLogstatus(Log.__UPDATE_DATA);
				logDAO.getData(l_logentity);
				
				AddCodeDetailDAO l_AddCodeDetailDAO = RemittanceDAOFactory.getAddCodeDetailDAO(getConnection());
				AddCodeDetail l_AddCodeDetail = l_AddCodeDetailDAO.findLastByAddCodeId(l_AddCode.getAddCodeId());
	            if( l_AddCode.getAddCodeId() == l_AddCodeDetail.getAddCodeId()){
				   l_AddCodeDetail.setReceiver(entity.getReceiver());
				   l_AddCodeDetail.setNRC(entity.getNrc());
				   l_AddCodeDetail.setAddress(entity.getAddress());  
				   l_AddCodeDetailDAO.update(l_AddCodeDetail);
				   
				   l_logentity.setTablename("AddCodeDetail");
					l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
					l_logentity.setKey(l_AddCodeDetail.getAddCodeDetailId());
					l_logentity.setLogstatus(Log.__UPDATE_DATA);
					logDAO.getData(l_logentity);
	            }
			  
			}
				if(entity.getPaidBy() == 0 && !entity.getBankCode().equals("")){
					BankDAO bankDAO = RemittanceDAOFactory.getBankDAO(getConnection());
					Bank l_Bank = bankDAO.findByCode(entity.getBankCode(), entity.getBranchId());
					if(l_Bank == null)
						throw new DataNotFoundException("Bank Code does not exist.", Bank.class);
					entity.setPaidBy(l_Bank.getBankId());
				}
				
				entity.setReceivedId(KeyGenerator.generateKey(getConnection()));
				String voucher = CashReceivedSerialMgr.generateCashReceivedNo(getConnection(), entity.getBranchId(), entity.getReceivedDate());
				entity.setReceiptNo(voucher);
				
				gotoGL(entity, GL_STATUS.CREATE);
				
				rmtCashReceiveDAO.create(entity);
				
				l_logentity.setTablename("RemittanceCashReceived");
				l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
				l_logentity.setKey(entity.getReceivedId());
				l_logentity.setUserid(entity.getCreatedBy());
				l_logentity.setLogstatus(Log.__SAVE_DATA);
				logDAO.getData(l_logentity);
				
				return voucher;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void update(RemittanceCashReceived entity) throws Exception{
		openConnection();
		try {
			rmtCashReceiveDAO = RemittanceDAOFactory.getRemittanceCashReceivedDAO(getConnection());
			BankDAO bankDAO = RemittanceDAOFactory.getBankDAO(getConnection());
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			
			if(entity.getPaidBy() == 0 && !entity.getBankCode().equals("")){
				Bank l_Bank = bankDAO.findByCode(entity.getBankCode(), entity.getBranchId());
				if(l_Bank == null)
					throw new DataNotFoundException("Bank Code does not exist.");
			}
			
			gotoGL(entity, GL_STATUS.CREATE);
			
			rmtCashReceiveDAO.udpate(entity);
			
			l_logentity.setTablename("RemittanceCashReceived");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(entity.getReceivedId());
			l_logentity.setUserid(entity.getModifiedBy());
			l_logentity.setLogstatus(Log.__UPDATE_DATA);
			logDAO.getData(l_logentity);
			
			AddCodeDAO l_AddCodeDAO = RemittanceDAOFactory.getAddCodeDAO(getConnection());
			AddCodeDetailDAO l_AddCodeDetailDAO = RemittanceDAOFactory.getAddCodeDetailDAO(getConnection());
			AddCode l_AddCode = l_AddCodeDAO.findByCode("_", entity.getBranchId());
			entity.setAddCodeId(l_AddCode.getAddCodeId());
			l_AddCode.setSender(entity.getSender());
			l_AddCodeDAO.update(l_AddCode);
			
			l_logentity.setTablename("AddCode");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(l_AddCode.getAddCodeId());
			l_logentity.setUserid(l_AddCode.getUserid());
			l_logentity.setLogstatus(Log.__UPDATE_DATA);
			logDAO.getData(l_logentity);
			
			AddCodeDetail l_AddCodeDetail = l_AddCodeDetailDAO.findLastByAddCodeId(l_AddCode.getAddCodeId());
            if( l_AddCode.getAddCodeId() == l_AddCodeDetail.getAddCodeId()){
			   l_AddCodeDetail.setReceiver(entity.getReceiver());
			   l_AddCodeDetail.setNRC(entity.getNrc());
			   l_AddCodeDetail.setAddress(entity.getAddress());  
			   l_AddCodeDetailDAO.update(l_AddCodeDetail);
			   
			   l_logentity.setTablename("AddCodeDetail");
				l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
				l_logentity.setKey(l_AddCodeDetail.getAddCodeDetailId());
				l_logentity.setLogstatus(Log.__UPDATE_DATA);
				logDAO.getData(l_logentity);
            }
				
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void delete(long key, long userId, Date deletedDate)throws Exception{
		openConnection();
		try {
			beginTransaction();
			RemittanceCashReceivedArchiveDAO l_archiveDAO = RemittanceDAOFactory.getRemittanceCashReceivedArchiveDAO(getConnection());
			rmtCashReceiveDAO = RemittanceDAOFactory.getRemittanceCashReceivedDAO(getConnection());
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			
			RemittanceCashReceived l_data = rmtCashReceiveDAO.find(key);
			if(l_data == null)
				throw new DataNotFoundException("Record not found.");
			
			l_logentity.setTablename("RemittanceCashReceived");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(key);
			l_logentity.setUserid(userId);
			l_logentity.setLogstatus(Log.__DELETE_DATA);
			logDAO.getData(l_logentity);
			
			gotoGL(l_data, GL_STATUS.DELETE);
			
			rmtCashReceiveDAO.delete(key);
			l_data.setModifiedBy(userId);
			l_data.setModifiedDate(deletedDate);
			l_data.setStatus(CommonConstants.RecordStatus.delete);
			l_archiveDAO.create(l_data);
			commintTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void confrim(ArrayList<Long> keyList,ArrayList<RemittanceCashReceived> rmtCRList, long userId)throws Exception{
		openConnection();
		try {
			beginTransaction();
			RemittanceCashReceivedArchiveDAO l_archiveDAO = RemittanceDAOFactory.getRemittanceCashReceivedArchiveDAO(getConnection());
			rmtCashReceiveDAO = RemittanceDAOFactory.getRemittanceCashReceivedDAO(getConnection());
			Date l_confirmedDate = new Date();
			long l_batchId = keyList.get(0);
			
			for (Long l_key : keyList) {
				RemittanceCashReceived l_data = rmtCashReceiveDAO.find(l_key);
				if(l_data == null)
					throw new DataNotFoundException("Record not found.");
				rmtCashReceiveDAO.delete(l_key);
				l_data.setStatus(RMTRecordStatus.confirmed);
				l_data.setModifiedBy(userId);
				l_data.setModifiedDate(l_confirmedDate);
				l_data.setBatchId(l_batchId);
				l_archiveDAO.create(l_data);
				
				rmtCRList.add(l_data);
			}
			
			CashReceivedDownloadLog l_log = new CashReceivedDownloadLog();
			l_log.setBatchId(l_batchId);
			l_log.setFromReceiptNo(rmtCRList.get(0).getReceiptNo());
			l_log.setBranchId(rmtCRList.get(0).getBranchId());
			l_log.setToReceiptNo(rmtCRList.get(rmtCRList.size() - 1).getReceiptNo());
			l_log.setDownloaded(false);
			
			CashReceivedDownloadLogDAO l_CashReceivedDownlog = RemittanceDAOFactory.getCashReceivedDownloadLogDAO(getConnection());
			l_CashReceivedDownlog.create(l_log);
			
			commintTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void updateDownloadStats(long batchId)throws Exception{
		openConnection();
		try {
			CashReceivedDownloadLogDAO l_logDAO = RemittanceDAOFactory.getCashReceivedDownloadLogDAO(getConnection());
			l_logDAO.updateDownloadStatus(batchId);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public List<RemittanceCashReceived> findByDateRange(Date fromDate, Date toDate, long branchId) throws Exception{
		openConnection();
		try {
			RemittanceCashReceivedArchiveDAO rmtArchiveDAO = RemittanceDAOFactory.getRemittanceCashReceivedArchiveDAO(getConnection());
			return rmtArchiveDAO.findByDateRange(fromDate, toDate, branchId);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public RemittanceCashReceived find(long key)throws Exception{
		openConnection();
		try {
			rmtCashReceiveDAO = RemittanceDAOFactory.getRemittanceCashReceivedDAO(getConnection());
			return rmtCashReceiveDAO.find(key);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public RemittanceCashReceived find(String receiptNo)throws Exception{
		openConnection();
		try {
			rmtCashReceiveDAO = RemittanceDAOFactory.getRemittanceCashReceivedDAO(getConnection());
			return rmtCashReceiveDAO.find(receiptNo);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public AddCodeNDetail findAddCodeNDetailByCode(String code, long branchId)throws Exception{
		openConnection();
		try {	
			AddCodeDAO l_AddCodeDAO = RemittanceDAOFactory.getAddCodeDAO(getConnection());
			AddCodeDetailDAO l_AddCodeDetailDAO = RemittanceDAOFactory.getAddCodeDetailDAO(getConnection());
			AddCode l_AddCode = l_AddCodeDAO.findByCode(code, branchId);
			AddCodeNDetail l_AddCodeNDetail = null;
			if(l_AddCode != null){
				rmtCashReceiveDAO = RemittanceDAOFactory.getRemittanceCashReceivedDAO(getConnection());
				l_AddCodeNDetail = rmtCashReceiveDAO.getLatestAddCodeNDetail(l_AddCode.getAddCodeId());
				if(l_AddCodeNDetail == null){
					l_AddCodeNDetail = new AddCodeNDetail();
					l_AddCodeNDetail.setAddCodeId(l_AddCode.getAddCodeId());
					l_AddCodeNDetail.setSender(l_AddCode.getSender());
					l_AddCodeNDetail.setCode(l_AddCode.getCode());
					
					AddCodeDetail l_AddCodeDetail = l_AddCodeDetailDAO.findLastByAddCodeId(l_AddCode.getAddCodeId());
					l_AddCodeNDetail.setReceiver(l_AddCodeDetail.getReceiver());
					l_AddCodeNDetail.setNrc(l_AddCodeDetail.getNRC());
					l_AddCodeNDetail.setAddress(l_AddCodeDetail.getAddress());
				}
				else{
					l_AddCodeNDetail.setCode(l_AddCode.getCode());
				}
			}else{
				return null;
			}
			
			return l_AddCodeNDetail;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public RemittanceCashReceivedDataList findRmtCashReceived(CriteriaParams cri)throws Exception{
		openConnection();
		try {
			rmtCashReceiveDAO = RemittanceDAOFactory.getRemittanceCashReceivedDAO(getConnection());
			return rmtCashReceiveDAO.findRmtCashReceived(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public RemittanceCashReceivedDataList findRmtCashReceivedArchive(CriteriaRMParams cri)throws Exception{
		openConnection();
		try {

			RemittanceCashReceivedArchiveDAO l_rmtCashReceivedDAO = RemittanceDAOFactory.getRemittanceCashReceivedArchiveDAO(getConnection());
			if(cri.isIs_All() == false)
				return l_rmtCashReceivedDAO.findRmtCashReceived(cri);
			else
				return l_rmtCashReceivedDAO.findAllRmtCashReceived(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public RemittanceCashReceivedDataList findRmtCashReceivedArchive(long batchId)throws Exception{
		openConnection();
		try {
			RemittanceCashReceivedArchiveDAO l_rmtCashReceivedDAO = RemittanceDAOFactory.getRemittanceCashReceivedArchiveDAO(getConnection());
			return l_rmtCashReceivedDAO.findRmtCashReceived(batchId);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public int getTotalReceipt(long branchId)throws Exception{
		openConnection();
		try {
			rmtCashReceiveDAO = RemittanceDAOFactory.getRemittanceCashReceivedDAO(getConnection());
			return rmtCashReceiveDAO.getTotalReceipt(branchId);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public CashReceivedDownloadLogDataList findCashReceivedDownloadLog(CriteriaParams cri)throws Exception{
		openConnection();
		try {
			CashReceivedDownloadLogDAO l_logDAO = RemittanceDAOFactory.getCashReceivedDownloadLogDAO(getConnection());
			return l_logDAO.findLog(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	//MBC Begin
	// For Report
	public JasperPrint prepareCashReceiptReport(CashReceiptRpt info)
			throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			rmtCashReceiveDAO = RemittanceDAOFactory.getRemittanceCashReceivedDAO(dbManager.getConnection());
			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(rmtCashReceiveDAO.prepareCashReceiptReport(info));
			JasperReport l_jsRpt = null;
			if(info.getGroupby().equals("PaidCurrencyID"))
				l_jsRpt = (JasperReport) JRLoader
				.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/CashReceiptByPaidCurRpt.jasper");
			else
				l_jsRpt = (JasperReport) JRLoader
					.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/CashReceiptRpt.jasper");
	
			HashMap<String, Object> l_params = new HashMap<String, Object>();
			l_params.put("company", "mbc");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;
	}
	
	
	public void gotoGL(RemittanceCashReceived l_entity, GL_STATUS status) throws Exception {
		
		currencyRateDAO = CommonDAOFactory.getCurrencyRateDAO(getConnection());
		branchCOADAO = RemittanceDAOFactory.getBranchCOADAO(getConnection());
		generalCOADAO = RemittanceDAOFactory.getGeneral_COADAO(getConnection());
		
		GeneralLedgerMgr l_glmgr = new GeneralLedgerMgr(this.sessionId);
		l_glmgr.deleteGL(l_entity.getReceiptNo() + "-RM");
		  
		if(status == GL_STATUS.CREATE) {
			GeneralLedger _glHeader = new GeneralLedger();
			_glHeader.setGlKey(l_glmgr.getGLKey());
			_glHeader.setSlip_ID(l_entity.getReceiptNo() + "-RM");
			_glHeader.setSlipDate(l_entity.getReceivedDate());
			_glHeader.setSystemDate(new Date());
			_glHeader.setStatus("JH");
			_glHeader.setSrNo(1);
			_glHeader.setOffice_ID(l_entity.getBranchId());
			
			CurrencyRate l_currencyrate = currencyRateDAO.getCurrencyRateFromSourcetoHome(l_entity.getReceivedCurrenyId(), l_entity.getBranchId());
			
			General_COA l_generalCOA = generalCOADAO.find(l_entity.getBranchId());
			
			if(l_generalCOA == null)
				throw new DataNotFoundException("Plz fill general coa", General_COA.class);
			
			GeneralLedger _glHeaderGeneral = new GeneralLedger();
			_glHeaderGeneral.setGlKey(l_glmgr.getGLKey());
			_glHeaderGeneral.setSlip_ID(l_entity.getReceiptNo() + "-RM");
			_glHeaderGeneral.setSlipDate(l_entity.getReceivedDate());
			_glHeaderGeneral.setSystemDate(new Date());
			_glHeaderGeneral.setStatus("JRM");
			_glHeaderGeneral.setSrNo(1);
			_glHeaderGeneral.setOffice_ID(l_entity.getBranchId());
			_glHeaderGeneral.setCoaKey(l_generalCOA.getCashAccountID());
			_glHeaderGeneral.setDepartmentKey(l_generalCOA.getCashDepartmentID());
			_glHeaderGeneral.setCurrencyKey(l_entity.getReceivedCurrenyId());
			_glHeaderGeneral.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
			_glHeaderGeneral.setExchangeRate(l_currencyrate.getRate());
			_glHeaderGeneral.setSourceAmount(l_entity.getRemitAmount()+l_entity.getExtraFees());
			
			if(l_currencyrate.getRoper().equals("M"))
				_glHeaderGeneral.setHomeAmount((l_entity.getRemitAmount()+l_entity.getExtraFees()) * l_currencyrate.getRate());
			else
				_glHeaderGeneral.setHomeAmount((l_entity.getRemitAmount()+l_entity.getExtraFees()) / l_currencyrate.getRate());
			
			l_glmgr.createGL(_glHeaderGeneral);
			
			GeneralLedger _glGeneral = new GeneralLedger();
			_glGeneral.setGlKey(l_glmgr.getGLKey());
			_glGeneral.setSlip_ID(l_entity.getReceiptNo() + "-RM");
			_glGeneral.setSlipDate(l_entity.getReceivedDate());
			_glGeneral.setSystemDate(new Date());
			_glGeneral.setStatus("JRM");
			_glGeneral.setSrNo(2);
			_glGeneral.setOffice_ID(l_entity.getBranchId());
			_glGeneral.setCoaKey(l_generalCOA.getExtraFeesAccountID());
			_glGeneral.setDepartmentKey(l_generalCOA.getExtraFessDepartmentID());
			_glGeneral.setCurrencyKey(l_entity.getReceivedCurrenyId());
			_glGeneral.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
			_glGeneral.setExchangeRate(l_currencyrate.getRate());
			_glGeneral.setSourceAmount(-l_entity.getExtraFees());
			
			if(l_currencyrate.getRoper().equals("M"))
				_glGeneral.setHomeAmount(-(l_entity.getExtraFees() * l_currencyrate.getRate()));
			else
				_glGeneral.setHomeAmount(-(l_entity.getExtraFees() / l_currencyrate.getRate()));
			
			l_glmgr.createGL(_glGeneral);
			
			BranchCOA l_branchCOA = branchCOADAO.findByHostBranchID(l_entity.getBranchId(), l_entity.getIssuedBranchId());
			
			if(l_branchCOA == null)
				throw new DataNotFoundException("Plz fill BranchCOA", BranchCOA.class);
			
			GeneralLedger _gltobranch = new GeneralLedger();
			_gltobranch.setGlKey(l_glmgr.getGLKey());
			_gltobranch.setSlip_ID(l_entity.getReceiptNo() + "-RM");
			_gltobranch.setSlipDate(l_entity.getReceivedDate());
			_gltobranch.setSystemDate(new Date());
			_gltobranch.setCoaKey(l_branchCOA.getCurrentAccountID());
			_gltobranch.setDepartmentKey(l_branchCOA.getDepartmentID());
			_gltobranch.setCurrencyKey(l_entity.getReceivedCurrenyId());
			_gltobranch.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
			_gltobranch.setExchangeRate(l_currencyrate.getRate());
			_gltobranch.setSourceAmount(-l_entity.getRemitAmount());
			
			if(l_currencyrate.getRoper().equals("M"))
				_gltobranch.setHomeAmount(-(l_entity.getRemitAmount() * l_currencyrate.getRate()));
			else
				_gltobranch.setHomeAmount(-(l_entity.getRemitAmount() / l_currencyrate.getRate()));
			
			_gltobranch.setStatus("JRM");
			_gltobranch.setSrNo(3);
			_gltobranch.setOffice_ID(l_entity.getBranchId());
			
			l_glmgr.createGL(_gltobranch);
		l_glmgr.createGL(_glHeader);
		
	}
	}
	//MBC End
}
